<template>
  <div>
    <h2>Login</h2>
    <input v-model="account" placeholder="Account" />
    <input v-model="pwd" type="password" placeholder="Password" />
    <button @click="login">Login</button>
    <div>
      <!-- 登录表单 -->
      <router-link to="/register">Don't have an account? Register</router-link>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios';

const account = ref('');
const pwd = ref('');
const router = useRouter();

async function login() {



  try {
    const response = await axios.post('/api/login', {
      account: account.value,
      pwd: pwd.value
    });
    console.log("============="+response);
    if (response.data.code === 200) {
      console.log("============="+response.data.code);
      localStorage.setItem('user', JSON.stringify(response.data));
      router.push('/list');
    } else {
      alert(response.data.message);
    }
  } catch (error) {
    console.error(error);
    alert('Login failed');
  }
}
</script>
